<template>
  <div class="outermostLayer">
    <div class="search">
      <div class="searchForContent">
        <div>
          <el-cascader
            v-model="selectArea"
            :options="areaList"
            @change="handleChange"
            :props="{ emitPath: false }"
            :show-all-levels="false"
            clearable
          />
        </div>
        <div>
          <el-input
            v-model="searchJobName"
            style="width: 400px"
            placeholder="搜索职位名称"
            clearable
          />
        </div>
      </div>
      <div class="searchForBtu" @click="searchJob">
        <!-- <svg-icon
          icon-name="searchFor"
          style="width: 50px; height: 100%"
        ></svg-icon> -->
        搜索
      </div>
    </div>
    <div
      style="
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        min-height: 90vh;
        margin: auto;
      "
    >
      <el-empty
        description="此城市暂时无职位信息数据，请持续保持关注度！"
        v-if="total === 0"
      />
      <div
        class="container"
        v-for="(item, index) in jobPostings"
        :key="index"
        title="点击可查看详情"
        @click="toDetail(item.id)"
      >
        <div class="header">
          <!-- <div class="logo">111</div> -->
          <div class="block" style="margin-right: 20px; height: 50px">
            <el-avatar :size="80" :src="item.logo" />
          </div>
          <div class="right">
            <div class="company-name">{{ item.companyName }}</div>
            <div style="display: flex; flex-direction: column; width: 100%">
              <div style="display: flex; align-items: center">
                <div class="positionName">{{ item.positionName }}</div>
                <div class="position">{{ item.respPos }}</div>
              </div>
              <div
                style="
                  display: flex;
                  margin-top: 10px;
                  margin-bottom: 10px;
                  align-items: center;
                "
              >
                <div class="year">{{ item.experience }}</div>
                <div class="education">{{ item.education }}</div>
              </div>
              <div style="display: flex">
                <div class="salary">{{ item.salary }}</div>
                <div class="place">
                  {{ item.province }} <span v-if="item.province">-</span>
                  {{ item.city }} <span v-if="item.street">-</span>
                  {{ item.street }}
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="footer">
          <div class="classify">
            <ul>
              <li
                v-for="(item, index) in industries(item.industryType)"
                :key="index"
              >
                {{ item }}
              </li>
            </ul>
          </div>
          <div class="descibe">
            {{ item.content }}
          </div>
        </div>
      </div>
    </div>
    <div style="display: flex; justify-content: center; width: 100%">
      <el-pagination
        v-model:current-page="currentPage"
        :hide-on-single-page="true"
        :page-size="12"
        :size="size"
        :background="true"
        layout="total, prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, ref } from "vue";
import type { ComponentSize } from "element-plus";
import { Search } from "@element-plus/icons-vue";
import { getJobPostings, getJobPosting } from "@/api/position";
import Cookies from "js-cookie";
import { useRouter } from "vue-router";
import { useUserInfor } from "@/stores/userInfo";

const userStore = useUserInfor();
const router = useRouter();

//分页数据
const currentPage = ref(1);
const total = ref(0);
const size = ref<ComponentSize>("default");

//获取职位推荐信息
const jobPostings = ref([]);
const queryJobPositings = async () => {
  if (Cookies.get("token")) {
    const res = await getJobPostings(currentPage.value, 12);
    jobPostings.value = res.data.list;
    total.value = res.data.total;
  } else {
    const res = await getJobPosting(currentPage.value, 12, "", "");
    jobPostings.value = res.data.list;
    total.value = res.data.total;
  }
  // jobPostings.value = res;
};
queryJobPositings();

// 使用computed来定义industries计算属性
const industries = (industryType) => {
  // 使用split方法将字符串转换为数组
  return industryType ? industryType.split("/") : [];
};

//分页变化
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  currentPage.value = val;
  queryJobPositings();
};

//根据职位名称或者地区搜索职位信息
const searchJobName = ref("");
const selectArea = ref("");

//接收地区，定义数组
function transformProvinceList(provinceList) {
  return provinceList.map((provinceItem) => {
    return {
      value: provinceItem.province,
      label: provinceItem.province,
      children: provinceItem.city.map((cityItem) => ({
        value: cityItem,
        label: cityItem,
      })),
    };
  });
}

const areaList = transformProvinceList(userStore.provinceList);

const handleChange = (value) => {
  console.log(value);
  console.log(selectArea.value);
};

const searchJob = async () => {
  const res = await getJobPosting(
    currentPage.value,
    12,
    searchJobName.value,
    selectArea.value
  );
  jobPostings.value = res.data.list;
  total.value = res.data.total;
};

//跳转到详情页
const toDetail = (id: number) => {
  router.push(`/position/jobDetails?id=${id}`);
};
</script>

<style lang="scss" scoped>
ul {
  padding-left: 0;
  list-style-type: none;
}

.outermostLayer {
  min-hegiht: 90vh;
  display: flex;
  flex-wrap: wrap;
  min-width: 80%;
  flex-direction: column;
  align-items: center;
}

.search {
  width: 700px;
  height: 50px;
  background-color: #b7b7b7;
  border-radius: 10px;
  margin-bottom: 20px;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 5px;

  .searchForContent {
    display: flex;
    width: 600px;
    height: 50px;
    background-color: #fff;
    border-radius: 10px;
    align-items: center;
    flex-direction: row;
  }

  .searchForBtu {
    width: 100px;
    height: 50px;
    font-size: 28px;
    font-weight: 900;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
  }
}

.container {
  display: flex;
  flex-direction: column;
  padding: 20px;
  border: 1px solid #ccc;
  width: 300px;
  //   box-shadow: 20px 20px 50px #000;
  /* filter: blur(6px); */
  // background: rgba(240, 235, 227, 0.3);
  background: #8ca9aa;
  backdrop-filter: blur(10px);
  border-top: 1px solid #ffffff50;
  border-left: 1px solid #ffffff50;
  border-radius: 15px;
  margin-left: 20px;
  margin-bottom: 20px;
  cursor: pointer;
}

.container:hover {
  background: rgba(140, 169, 170, 0.5);
}

.header {
  display: flex;
}

.right {
  width: 77%;
  display: flex;
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.logo {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  width: 70px;
  height: 70px;
  background-color: #ccc;
  border-radius: 50%;
  margin-right: 20px;
}

.company-name {
  color: #dcdcdc;
  width: 100%;
  font-size: 20px;
  margin-bottom: 10px;
}

.header .right .position,
.header .right .year {
  width: 50%;
  box-sizing: border-box;
  color: #fff;
}

.header .right .year {
  width: 83%;
}

.positionName {
  width: 83%;
  box-sizing: border-box;
  color: #fff;
}

.middle {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.place {
  width: 50%;
  margin-bottom: 10px;
  box-sizing: border-box;
  color: #fff;
}

.middle .right {
  width: 71%;
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  padding: 10px;
  box-sizing: border-box;
}

.education {
  width: 50%;
  box-sizing: border-box;
  color: #fff;
}

.salary {
  width: 83%;
  color: #fff;
  box-sizing: border-box;
}

.footer {
  color: #424242;
  display: flex;
  justify-content: space-between;
}

.footer .classify {
  width: 29%;
  padding: 10px;
  box-sizing: border-box;
}

.footer .descibe {
  width: 71%;
  padding: 5px 0 0 5px;
  box-sizing: border-box;
  display: -webkit-box;
  -webkit-line-clamp: 7;
  /* 限制显示 6 行 */
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

:deep(.el-select__wrapper) {
  align-items: center;
  border-radius: 10px 0 0 10px;
  box-sizing: border-box;
  cursor: pointer;
  display: flex;
  font-size: 14px;
  gap: 6px;
  line-height: 24px;
  min-height: 32px;
  position: relative;
  text-align: left;
  height: 50px;
  transform: translateZ(0);
  transition: var(--el-transition-duration);
}

:deep(.el-input__wrapper) {
  align-items: center;
  background-color: var(--el-input-bg-color, var(--el-fill-color-blank));
  background-image: none;
  // border-radius: 0 10px 10px 0;
  /* box-shadow: 0 0 0 1px var(--el-input-border-color, var(--el-border-color)) inset; */
  cursor: text;
  display: inline-flex;
  flex-grow: 1;
  justify-content: center;
  /* padding: 1px 11px; */
  transform: translateZ(0);
  transition: var(--el-transition-box-shadow);
  height: 48px;
}
</style>
